package myky.com.doctorapp.utils;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.github.nukc.stateview.Constant;

import com.youth.banner.loader.ImageLoader;

import myky.com.doctorapp.R;
import myky.com.doctorapp.api.Address;
import myky.com.doctorapp.base.CMApplication;

/**
 * Title: ${NAME}$
 * Description: 优化图片的加载性能
 *
 * @author xiangyao
 * date 2018/8/2
 */
public class BannerImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        //用fresco加载图片简单用法，记得要写下面的createImageView方法
        Uri uri = UriUtils.getUriInstance().getUri(Constant.ONLINEPIC, Address.IMAGE + path);
        //运行时用来配置SimpleDraweeView 的工具  xml 可以通过属性来配置(占位图) 是我的网络太好了么，根本看不到进度啊
        GenericDraweeHierarchyBuilder genericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder(CMApplication.getAppContext().getResources());
        genericDraweeHierarchyBuilder.setPlaceholderImage(R.mipmap.ic_launcher);//占位图
        genericDraweeHierarchyBuilder.setFailureImage(R.mipmap.ic_launcher);
        //官方api 加载图片的监听 这里用来实现预览图(由模糊到清晰(渐变效果)
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setProgressiveRenderingEnabled(true)
                .setLocalThumbnailPreviewsEnabled(true)
                .build();
        //SimpleDraweeView 加载图片的控制器
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(((SimpleDraweeView) imageView).getController())
                .setAutoPlayAnimations(true)
                .build();
        ((SimpleDraweeView) imageView).setHierarchy(genericDraweeHierarchyBuilder.build());
        ((SimpleDraweeView) imageView).setController(controller);
    }

    @Override
    public ImageView createImageView(Context context) {
        //使用fresco，需要创建它提供的ImageView，当然你也可以用自己自定义的具有图片加载功能的ImageView
        SimpleDraweeView simpleDraweeView = new SimpleDraweeView(context);
        return simpleDraweeView;
    }
}
